#include <SPI.h>
#include <SD.h>
#include <EEPROM.h>

File blackbox;

void setup()
{
  Serial.begin(100000, SERIAL_8N2);
  int address = EEPROM.read(0);
  if (address == 0xFF)
  {
    address = 0;
    EEPROM.write(0, address);
  }
  else
  {
    address += 1;
    if (address >= 10)
    {
      address = 0;
    }
  }

  SD.begin(10);
  switch (address)
  {
    case 0:
    blackbox = SD.open("1.csv", FILE_WRITE);
    break;

    case 1:
    blackbox = SD.open("2.csv", FILE_WRITE);
    break;

    case 2:
    blackbox = SD.open("3.csv", FILE_WRITE);
    break;

    case 3:
    blackbox = SD.open("4.csv", FILE_WRITE);
    break;

    case 4:
    blackbox = SD.open("5.csv", FILE_WRITE);
    break;

    case 5:
    blackbox = SD.open("6.csv", FILE_WRITE);
    break;

    case 6:
    blackbox = SD.open("7.csv", FILE_WRITE);
    break;

    case 7:
    blackbox = SD.open("8.csv", FILE_WRITE);
    break;

    case 8:
    blackbox = SD.open("9.csv", FILE_WRITE);
    break;

    case 9:
    blackbox = SD.open("10.csv", FILE_WRITE);
    break;
  }
}

void loop()
{
  while (Serial.available())
  {
    blackbox.write(Serial.read());
  }
}
